<cfcomponent displayname="ElementBase" hint="This is an abstract class for Scripts, Styles, and any other ScriptWriter Elements that come into being.">

	
	<cfproperty name="sourcePath" type="string" />
	<cfproperty name="outputPath" type="string" />
	<cfproperty name="sourceFileFullExists" type="boolean" />
	<cfproperty name="sourceFileMinExists" type="boolean" />
	<cfproperty name="sourceFull" type="string" />
	<cfproperty name="sourceMin" type="string" />
	<cfproperty name="sourceFullIsLoaded" type="boolean" />
	<cfproperty name="minify" type="boolean" />
	<cfproperty name="compressor" type="any" />
	<cfproperty name="externalOutputFormat" type="string" />
	<cfproperty name="inlineOutputFormat" type="string" />
	<cfset variables.Instance = {} />


	<!--- init() --->
	<cffunction name="init" access="public" returntype="ScriptWriter.ElementBase">
		<cfargument name="sourcePath" type="string" required="no" default="">
		<cfargument name="src" type="string" required="no" default="">
		<cfargument name="minify" type="boolean" required="no" default="false">
		<cfargument name="outputPath" type="string" required="no" default="">
		<cfargument name="compressor" type="any" required="no" default="" />
		<cfscript>
		
			setSourcePath(arguments.sourcePath);
			setSource("full",arguments.src);
			setSource("min","");
			setMinify(arguments.minify);
			setOutputPath(arguments.outputPath);
			setCompressor(arguments.compressor);
			determineFileExistance("full");
			determineFileExistance("min");
			setSourceFullIsLoaded(false);
			
			if (getMinify() AND (NOT IsObject(getCompressor()))) {
				// throw an error.
				throw("You the set the element #arguments.path# to be minified, but the associated compressor is not an object. It may not have been intialized with ScriptWriter.");
			}
			
			// If getMinify() is TRUE
			// and the full source files DOES exists
			// and an output path has been set
			// and the minified file does not already exist
			// then run compress, which loads the full source, minifies it,
			// and then output that source as a new file.
			if (getMinify() AND getExists("full") AND (getOutputPath() NEQ "") AND (NOT getExists("min"))) {
				setSourceMin(compress());
				writeFile();
			}
			
		</cfscript>
		<cfreturn this />
	</cffunction>
	
	
	<!--- getSourcePath() --->
	<cffunction name="getSourcePath" access="public" returntype="string">
		<cfreturn variables.Instance["sourcePath"] />
	</cffunction>
	
	
	<!--- setSourcePath() --->
	<cffunction name="setSourcePath" access="public" returntype="string">
		<cfargument name="sPath" type="string" required="yes" />
		<cfset variables.Instance["sourcePath"] = arguments.sPath />
	</cffunction>
	
	
	<!--- getOutputPath() --->
	<cffunction name="getOutputPath" access="public" returntype="string">
		<cfreturn variables.Instance["outputPath"] />
	</cffunction>
	
	
	<!--- setOutputPath() --->
	<cffunction name="setOutputPath" access="public" returntype="string">
		<cfargument name="sPath" type="string" required="yes" />
		<cfset variables.Instance["outputPath"] = arguments.sPath />
	</cffunction>
	
	
	<!--- getExists() --->
	<cffunction name="getExists" access="public" returntype="boolean">
		<cfargument name="sFileType" type="string" required="yes" />
		<cfreturn variables.Instance["sourceFile#arguments.sFileType#Exists"] />
	</cffunction>
	
	
	<!--- setExists() --->
	<cffunction name="setExists" access="public" returntype="any">
		<cfargument name="sFileType" type="string" required="yes" />
		<cfargument name="blnExists" type="string" required="yes" />
		<cfset variables.Instance["sourceFile#arguments.sFileType#Exists"] = arguments.blnExists />
	</cffunction>
	
	
	<!--- getMinify() --->
	<cffunction name="getMinify" access="public" returntype="boolean">
		<cfreturn variables.Instance["minify"] />
	</cffunction>
	
	
	<!--- setMinify() --->
	<cffunction name="setMinify" access="public" returntype="any">
		<cfargument name="blnMinify" type="string" required="yes" />
		<cfset variables.Instance["minify"] = arguments.blnMinify />
	</cffunction>
	
	
	<!--- getCompressor() --->
	<cffunction name="getCompressor" access="public" returntype="any">
		<cfreturn variables.Instance["compressor"] />
	</cffunction>
	
	
	<!--- setCompressor() --->
	<cffunction name="setCompressor" access="public" returntype="any">
		<cfargument name="oCompressor" type="any" required="yes" />
		<cfset variables.Instance["compressor"] = arguments.oCompressor />
	</cffunction>
	
	
	<!--- determineFileExistance() --->
	<cffunction name="determineFileExistance" access="public" returntype="any">
		<cfargument name="sFileType" type="string" required="yes" />
		<cfif (arguments.sFileType EQ "full")>
			<cfset setExists(arguments.sFileType,FileExists(ExpandPath(getSourcePath()))) />
		<cfelse>
			<cfset setExists(arguments.sFileType,FileExists(ExpandPath(getOutputPath()))) />
		</cfif>
	</cffunction>
	
	
	<!--- getSourceFull() --->
	<cffunction name="getSourceFull" access="public" returntype="string">
		<cfscript>
			if (NOT getSourceFullIsLoaded()) {
				loadSource("full");
			}
			return variables.Instance["sourceFull"];
		</cfscript>
	</cffunction>
	
	
	<!--- setSourceFull() --->
	<cffunction name="setSourceFull" access="public" returntype="string">
		<cfargument name="sSource" type="string" required="yes" />
		<cfset variables.Instance["sourceFull"] = arguments.sSource />
	</cffunction>
	
	
	<!--- getSourceMin() --->
	<cffunction name="getSourceMin" access="public" returntype="string">
		<cfreturn variables.Instance["sourceMin"] />
	</cffunction>
	
	
	<!--- setSourceMin() --->
	<cffunction name="setSourceMin" access="public" returntype="string">
		<cfargument name="sSource" type="string" required="yes" />
		<cfset variables.Instance["sourceMin"] = arguments.sSource />
	</cffunction>
	
	
	<!--- getSource() --->
	<cffunction name="getSource" access="public" returntype="string">
		<cfargument name="sSourceType" type="string" required="yes" />
		
		<cfif (arguments.sSourceType EQ "Full")>
			<cfif (NOT getSourceFullIsLoaded())>
				<cfset loadSource("full") />
				<cfset setSourceFullIsLoaded(true) />
			</cfif>
		</cfif>			
		
		<cfreturn variables.Instance["source#sSourceType#"] />
	</cffunction>
	
		
	<!--- getPreferredSource() --->
	<cffunction name="getPreferredSource" access="public" returntype="string">
		<cfif (getMinify())>
			<cfreturn getSourceMin() />
		<cfelse>
			<cfreturn getSourceFull() />
		</cfif>
	</cffunction>
	
	
	<!--- getPreferredPath() --->
	<cffunction name="getPreferredPath" access="public" returntype="string">
		<cfif (getMinify())>
			<cfreturn getOutputPath() />
		<cfelse>
			<cfreturn getSourcePath() />
		</cfif>
	</cffunction>
	
	
	<!--- setSource() --->
	<cffunction name="setSource" access="public" returntype="string">
		<cfargument name="sSourceType" type="string" required="yes" />
		<cfargument name="sSource" type="string" required="yes" />
		<cfset variables.Instance["source#sSourceType#"] = arguments.sSource />
	</cffunction>
	
	
	<!--- loadSource() --->
	<cffunction name="loadSource" access="public" returntype="string">
		<cfargument name="sSourceType" type="string" required="yes" />
		
		<cfif (arguments.sSourceType EQ "full")>
			<cfif getExists(arguments.sSourceType)>
				<cfset setSource(arguments.sSourceType,FileRead(ExpandPath(getSourcePath()))) />
			<cfelse>
				<cfset setSource(arguments.sSourceType,"/* File: #getSourcePath()# was missing */") />
			</cfif>
		<cfelse>
			<cfif getExists(arguments.sSourceType)>
				<cfset setSource(arguments.sSourceType,FileRead(ExpandPath(getOutputPath()))) />
			<cfelse>
				<cfset setSource(arguments.sSourceType,"/* File: #getOutputPath()# was missing */") />
			</cfif>
		</cfif>
		
		
	</cffunction>
	
	
	<!--- getSourceFullIsLoaded() --->
	<cffunction name="getSourceFullIsLoaded" access="public" returntype="any">
		<cfreturn variables.Instance["sourceFullIsLoaded"] />
	</cffunction>
	
	
	<!--- setSourceFullIsLoaded() --->
	<cffunction name="setSourceFullIsLoaded" access="public" returntype="any">
		<cfargument name="blnState" type="boolean" required="yes" />
		<cfset variables.Instance["sourceFullIsLoaded"] = Arguments.blnState />
	</cffunction>
	
	
	<!--- getExternalOutputFormat() --->
	<cffunction name="getExternalOutputFormat" access="public" returntype="any">
		<cfreturn variables.Instance["externalOutputFormat"] />
	</cffunction>
	
	
	<!--- setExternalOutputFormat() --->
	<cffunction name="setExternalOutputFormat" access="public" returntype="any">
		<cfargument name="sOutputRefFormat" type="string" required="yes" />
		<cfset variables.Instance["externalOutputFormat"] = Arguments.sOutputRefFormat />
	</cffunction>
	
	
	<!--- getInternalOutputFormat() --->
	<cffunction name="getInlineOutputFormat" access="public" returntype="any">
		<cfreturn variables.Instance["inlineOutputFormat"] />
	</cffunction>
	
	
	<!--- setInternalOutputFormat() --->
	<cffunction name="setInlineOutputFormat" access="public" returntype="any">
		<cfargument name="sOutputRefFormat" type="string" required="yes" />
		<cfset variables.Instance["inlineOutputFormat"] = Arguments.sOutputRefFormat />
	</cffunction>
	
	
	<!--- getOutputFormat()--->
	<cffunction name="getOutputFormat" access="public" returntype="any">
		<cfargument name="sMode" type="string" required="yes" />
		<cfscript>
			if (arguments.sMode EQ "external") {
				return getExternalOutputFormat();	
			}
			else if (arguments.sMode EQ "inline") {
				return getInlineOutputFormat();
			}
		</cfscript>
	</cffunction>
	
	
	<!--- compress() --->
	<cffunction name="compress" access="public" returntype="any">

		<cfscript>
			if (NOT getSourceFullIsLoaded()) {
				loadSource("full");
			}
			return getCompressor().compress(getSourceFull());
		</cfscript>
		
	</cffunction>
	
	
	<!--- writeFile() --->
	<cffunction name="writeFile" access="public" returntype="any">
		<cfscript>
			FileWrite(ExpandPath(getOutputPath()),getSourceMin());
		</cfscript>
	</cffunction>
	
		
</cfcomponent>